package cn.iocoder.yudao.module.pay.controller.admin.packageorder;

import cn.iocoder.yudao.module.pay.api.notify.dto.PayOrderNotifyReqDTO;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.annotation.security.PermitAll;
import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;
import static cn.iocoder.yudao.framework.web.core.util.WebFrameworkUtils.getLoginUserId;

import cn.iocoder.yudao.module.pay.controller.admin.packageorder.vo.*;
import cn.iocoder.yudao.module.pay.dal.dataobject.packageorder.PackageOrderDO;
import cn.iocoder.yudao.module.pay.service.packageorder.PackageOrderService;

@Tag(name = "管理后台 - 套餐订单")
@RestController
@RequestMapping("/pay/package-order")
@Validated
public class PackageOrderController {

    @Resource
    private PackageOrderService packageOrderService;

//    @PostMapping("/create")
//    @Operation(summary = "创建套餐订单")
//    @PreAuthorize("@ss.hasPermission('pay:package-order:create')")
//    public CommonResult<Long> createPackageOrder(@Valid @RequestBody PackageOrderSaveReqVO createReqVO) {
//        return success(packageOrderService.createPackageOrder(getLoginUserId(), createReqVO));
//    }

//    @PutMapping("/update")
//    @Operation(summary = "更新套餐订单")
//    @PreAuthorize("@ss.hasPermission('pay:package-order:update')")
//    public CommonResult<Boolean> updatePackageOrder(@Valid @RequestBody PackageOrderSaveReqVO updateReqVO) {
//        packageOrderService.updatePackageOrder(updateReqVO);
//        return success(true);
//    }

    @PostMapping("/update-paid")
    @Operation(summary = "更新套餐订单为已支付") // 由 pay-module 支付服务，进行回调，可见 PayNotifyJob
    @PermitAll // 无需登录，安全由 PackageOrderService 内部校验实现
    public CommonResult<Boolean> updatePackageOrderPaid(@RequestBody PayOrderNotifyReqDTO notifyReqDTO) {
        packageOrderService.updatePackageOrderPaid(Long.valueOf(notifyReqDTO.getMerchantOrderId()),
                notifyReqDTO.getPayOrderId());
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除套餐订单")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('pay:package-order:delete')")
    public CommonResult<Boolean> deletePackageOrder(@RequestParam("id") Long id) {
        packageOrderService.deletePackageOrder(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得套餐订单")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('pay:package-order:query')")
    public CommonResult<PackageOrderRespVO> getPackageOrder(@RequestParam("id") Long id) {
        PackageOrderDO packageOrder = packageOrderService.getPackageOrder(id);
        return success(BeanUtils.toBean(packageOrder, PackageOrderRespVO.class));
    }

    @PostMapping("/page")
    @Operation(summary = "获得套餐订单分页")
    @PreAuthorize("@ss.hasPermission('pay:package-order:query')")
    public CommonResult<PageResult<PackageOrderRespVO>> getPackageOrderPage(@Valid @RequestBody PackageOrderPageReqVO pageReqVO) {
        return success(packageOrderService.getPackageOrderPage(pageReqVO));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出套餐订单 Excel")
    @PreAuthorize("@ss.hasPermission('pay:package-order:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportPackageOrderExcel(@Valid PackageOrderPageReqVO pageReqVO,
                                        HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<PackageOrderRespVO> list = packageOrderService.getPackageOrderPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "套餐订单.xls", "数据", PackageOrderRespVO.class, list);
    }

}